<?php
namespace app\common\util;

class Sms
{
    /**
     * 随机生成6位短信验证码
     */
    public static function get_send_code($randLength = 6){
        $chars = '0123456789';
        $len = strlen($chars);
        $randStr = '';
        for ($i = 0; $i < $randLength; $i++) {
            $randStr.=$chars[rand(0, $len - 1)];
        }
        return $randStr;
    }


    /**
     * 短信发送接口
     *  xinlingshou
     *  fb07476959ac7671bdaf659d63b2462c
     *  http://api.sms.cn/sms/?ac=send&uid=xinlingshou&pwd=接口密码[获取密码]&template=100006&mobile=填写要发送的手机号&content={"code":"value"}
     * 将value替换成你发送的内容，如：content={"code":"测试0"}收到的内容：
     * 验证码：测试0，请及时完成验证。【云信】
     * content参数内容需要进行URL编码
     * 功能:      HTTP接口 发送短信
     * 说明:      http://api.sms.cn/mt/?uid=用户账号&pwd=MD5位32密码&mobile=号码&mobileids=号码编号&content=内容
     * sid 店铺ID
     * sms_tel 号码
     * sms_content 发送内容
     * sms_remark 备注
     * sms_code 6位验证码
     * type发送类型[0验证码,1发送文本]
     */
    public static function send_sms_code($sid, $sms_tel, $sms_code = 0, $sms_content = '', $sms_remark = '', $type = 0){
        $http = 'http://api.sms.cn/sms/'; //短信接口
        $uid = 'xinlingshou'; //C('SMS_UID');  //用户账号
        $pwd = 'fb07476959ac7671bdaf659d63b2462c';//C('SMS_PWD'); //密码
        $mobile = $sms_tel;  //号码，以英文逗号隔开
        $templateid = '100006';   //号码唯一编号
        $content = $sms_content; //内容

        $url= $http.'?ac=send&uid='.$uid.'&pwd='.$pwd.'&template='.$templateid.'&mobile='.$sms_tel.'&content={"code":"'.$sms_code.'"}';

        $res = Util::curl_get($url);
        $res = iconv('UTF-8','GB2312//IGNORE',$res);

        $sta = json_decode($res,1);

        $sms_date = time();
        $data = array(
            'sms_sid' => $sid,
            'sms_tel' => $sms_tel,
            'sms_content' => $sms_content,
            'sms_remark' => $sms_remark,
            'sms_code' => $sms_code,
            'sms_type' => $type,
            'sms_time' => $sms_date,
            'sms_overdue' => $sms_date + 900,
        );

        if ($sta['stat'] == 100) {
            $data['sms_status'] = 1;
            $data['sms_error'] = self::send_sms_error($sta['stat']);
            db('sms')->insert($data);
            return true;
        } else {
            $data['sms_status'] = 0;
            $data['sms_error'] = self::send_sms_error($sta['stat']);
            db('sms')->insert($data);
            return false;
        }
        return false;
    }

    /**
     * 校验验证码
     * @param unknown $post_mobile
     * @param unknown $post_sms_code
     * @return boolean
     */
    public static function check_sms_code($mobile, $sms_code){
        //验证验证码
        $con = [
            'sms_tel' => $mobile,
            'sms_use' => 0,
            'sms_overdue' => ['>', time()]
        ];

        $sms_info = db('sms')->where($con)->order('sms_overdue desc')->find();
        if ($sms_info['sms_code'] != $sms_code) {
            return false;
        } else {
            $res = db('sms')->where(['sms_tel' => $mobile, 'sms_use' => 0])->update(['sms_use' => 1]);
            if($res === false){
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * @param $code
     * @return mixed
     * 发送短信错误码
     */
    public static function send_sms_error($code) {
        $sms_code = array(
            '100' => '发送成功',
            '101' => '验证失败',
            '102' => '短信不足',
            '103' => '操作失败',
            '104' => '非法字符',
            '105' => '内容过多',
            '106' => '号码过多',
            '107' => '频率过快',
            '108' => '号码内容空',
            '109' => '账号冻结',
            '110' => '禁止频繁单条发送',
            '112' => '号码不正确',
            '120' => '系统升级',
        );
        return $sms_code[$code];
    }

}